﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HAMath;
using HA.Data;
namespace HA.ModFlow.Controls.Windows.DataOperation
{
    /// <summary>
    /// win2DInterOptions.xaml 的交互逻辑
    /// </summary>
    public partial class win2DInterOptions : Window
    {
        public win2DInterOptions(Project project)
        {
            InitializeComponent();
            this._proj = project;
            this.InterOption =(InterpolationOption) project.InterpolationOption2D.Clone ();
            this.DataContext = this.InterOption;
        }
        private Project _proj;

        public Project Proj
        {
            get { return _proj; }
            set { _proj = value; }
        }

        InterpolationOption _interOption = new InterpolationOption();

        public InterpolationOption InterOption
        {
            get { return _interOption; }
            set { _interOption = value; }
        }

    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            switch (this._interOption.Interpolatetype)
            {
                case InterpolateType.IDW:
                    this.rbtnIDW.IsChecked = true;
                    break;
                case InterpolateType.Kriging:
                    this.rbtnKring.IsChecked = true;
                    break;
                case  InterpolateType.CloughTocher:
                    this.rbtnClough.IsChecked = true;
                    break;
                case  InterpolateType.Linear:
                    this.rbtnLinear.IsChecked = true;
                    break;
                case InterpolateType.NaturalNeihbor:
                    this.rbtnNatural.IsChecked = true;
                    break;
            }
            isEnbleTruncate();
            if (this._interOption.IsTruncateToDataSetMinMax)
            {
                this.rbtnRange.IsChecked = true;
            }
            else 
            {
                this.rbtnMinMax.IsChecked = true;
            }
            
        }

    

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (this.rbtnClough.IsChecked.Value)
            {
                this._interOption.Interpolatetype = InterpolateType.CloughTocher;
            }
            if (this.rbtnIDW.IsChecked.Value)
            {
                this._interOption.Interpolatetype = InterpolateType.IDW;
            }
            if (this.rbtnKring.IsChecked.Value)
            {
                this._interOption.Interpolatetype = InterpolateType.Kriging;
            }
            if (this.rbtnLinear.IsChecked.Value)
            {
                this._interOption.Interpolatetype = InterpolateType.Linear;
            }
            if (this.rbtnNatural.IsChecked.Value)
            {
                this._interOption.Interpolatetype = InterpolateType.NaturalNeihbor;
            }


            this.DialogResult = true;
            this.Close();
        }

        private void btnKrigOption_Click(object sender, RoutedEventArgs e)
        {
            winKrigingOptions win = new winKrigingOptions(this .Proj,this .InterOption.KrigingOption);
            if (win.ShowDialog().Value)
            {
                //this.Proj.InterpolationOption2D.KrigingOption = win.KrigingOption;
                this.InterOption.KrigingOption = win.KrigingOption;
            }
        }

        private void chkTruncateValues_Click(object sender, RoutedEventArgs e)
        {
            isEnbleTruncate();
        }

        private void isEnbleTruncate()
        {
            if (this.chkTruncateValues.IsChecked.Value)
            {
                this.rbtnMinMax.IsEnabled = true;
                this.rbtnRange.IsEnabled = true;
                if (rbtnRange.IsChecked.Value)
                {
                    this.txtMax.IsEnabled = true;
                    this.txtMin.IsEnabled = true;
                }
                else
                {
                    this.txtMax.IsEnabled = false;
                    this.txtMin.IsEnabled = false;
                }
            }
            else
            {
                this.rbtnMinMax.IsEnabled = false;
                this.rbtnRange.IsEnabled = false;
                this.txtMax.IsEnabled = false;
                this.txtMin.IsEnabled = false;
            }
        }

        private void rbtnMinMax_Click(object sender, RoutedEventArgs e)
        {
            isEnbleTruncate();
        }

        private void rbtnRange_Click(object sender, RoutedEventArgs e)
        {
            isEnbleTruncate();
        }
      
    }

    public class ConvertToInterplateType : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return true;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string str = (string)parameter;
            HA.Data.InterpolateType type = InterpolateType.IDW;
            switch(str)
            {
                case "Linear":
                    type = InterpolateType.Linear;
                    break;
                case "CloughTocher":
                    type = InterpolateType.CloughTocher;
                    break ;
                case "IDW":
                    type = InterpolateType.IDW;
                    break;
                case "Kriging":
                    type = InterpolateType.Kriging;
                    break;
                case "NaturalNeihbor":
                    type = InterpolateType.NaturalNeihbor;
                    break;
            }
            return type;
        }
    }

}
